
var keyword;	//关键字
var category;	//类别
var title;	//标题
var parentId;	//父Id
var childList = [];	//子目录
var productList = [];	
var isNeedGetChild = false;	//是否要加载子类

var urlString;	//从社区服务进来所带的参数

var currentPage = 1;	//当前页数
var limit = 10;		//每页个数
var count = 0;	//总数
var sortname = '';	//排序，按销量、售价
var sort = '';	//升序、降序

apiready = function() {
    
   	keyword = api.pageParam.keyword||'';
   	category = api.pageParam.category||'';
   	title = api.pageParam.title||'';
   	parentId = api.pageParam.parentId||'';
   	urlString = api.pageParam.urlString;
   	childList = api.pageParam.childList||[];
   	isNeedGetChild = api.pageParam.isNeedGetChild||false;
   
   	
   	if(keyword){
   		$api.text($api.byId('title'), keyword);
   	}else if(category){
   		$api.text($api.byId('title'), category);
   	}else{
   		$api.text($api.byId('title'), title);
   	}
   	
   	
	//上拉
	api.addEventListener({
		name: 'scrolltobottom'
	}, function (ret, err) {
       
		if(limit < count){
			limit += 10;
			getList();
	               
		}else{
			api.toast({
				msg: '下面没有啦...',
				duration: 1000,
				location: 'bottom'
			});
		}
	});


   	//加载数据
   	getList();

}


function getList(){
	api.showProgress({
		title: '加载中...',
		modal: false
	});
	
	var url;
	
	if(urlString){
		url = '/app/getproparlist'+urlString+'&sortname='+sortname+'&sort='+sort+'&page='+currentPage+'&limit='+limit;
		
		isNeedGetChild = true;
	}
	else{
		url = '/app/getprolist?keyword='+keyword+'&category='+category+'&page='+currentPage+'&limit='+limit+'&poundId=&sortname='+sortname+'&sort='+sort+'&parentId='+parentId;
	}
	console.log(url);
	
	$api.get(serviceUrl+url,function(ret){
//		console.log(JSON.stringify(ret));
		if(!$api.text($api.byId('title')) && ret.parname){
			$api.text($api.byId('title'), ret.parname);
		}
	
	    productList = ret.datas;
	    count = ret.count;
	    
	   	if(isNeedGetChild){
	   		childList = ret.childs;
	   		isNeedGetChild = false;
	   	}

		showList(productList);
		//更新子目录UI
    	showChilds(childList);

	  
	},'json');
}


function showList(list){

	var aHtml = "";
	
	if(list.length > 0){
		
		for(var i = 0; i < list.length; i++){
		   
		   aHtml += '<div class="am-u-sm-12" style="border-bottom:1px solid #eaedf1;padding:0px 10px;height:85px">';
		   aHtml += '<table  border="0" width="100%" style="margin-top:8px;table-layout:fixed;">';
		   aHtml += '<tr>';
		   aHtml += '<td rowspan="2" valign="middle" style="width:70px;" onclick="enterProductInfo('+i+');">';
		   aHtml += '<img src="'+serviceUrl + list[i].proDefImg+'"  style="width:70px;height:70px;"/>';
		   aHtml += '</td>';
		   aHtml += '<td  colspan="2" width="60%" onclick="enterProductInfo('+i+');"  style="word-break:break-all;font-size:14px"  valign="top">';
		   aHtml += list[i].name;
		   aHtml += '</td>';
		   aHtml += '</tr>';
		   
		   aHtml += '<tr style="color:#f40">';
		   aHtml += '<td valign="bottom">￥'+ list[i].price + '</td>';
		   
		   if(list[i].type !== '服务产品'){
			   aHtml += '<td align="right">';
			   aHtml += '<img src="images/addcart.png" style="width:23px" onclick="addCart('+i+')"/>';
			   aHtml += '</td>';
		   }
		   
		   aHtml += '</tr>';
		   aHtml += '</table>';
		   aHtml += '</div>';
		
		}
		
	}else{
	     aHtml += '<div  class="am-u-sm-12" style="margin-top:10px;text-align:center">暂无数据...</div>'
	}
	
	$api.html($api.byId('productList'), aHtml);

	api.hideProgress();
}


//子类
function showChilds(list){

	var aHtml='';
   
	if(list.length > 0){
		for(var i=0;i<list.length;i++){
			aHtml+='<li onclick="clickChildItem('+i+')">';
			aHtml+='<a>'+list[i].name+'</a>';
			aHtml+='</li>';
		}
	}else{
		aHtml+='<li><a>暂无目录...</a></li>';
	}
   
	$api.html($api.byId('childs_ul'), aHtml);

};


function clickChildItem(index){
	var child = childList[index];
	
	category = child.name;
	urlString = '';
	
	$api.text($api.byId('title'), category);
	getList();
}


function addCart(index){
	var product = productList[index];
	
	addCratItem(product._id);
}


function enterProductInfo(index){
	var product = productList[index];
	
	openWin('productinfo',{'product':product});
}


function clickSale(){
	
	var classname = $api.byId('i1').className;
	
	if(classname == 'am-icon-angle-down'){
		$api.removeCls($api.byId('i1'), 'am-icon-angle-down');
		$api.addCls($api.byId('i1'), 'am-icon-angle-up');
		
		sortname = 'sales';
		sort = 'asc';
		
		getList();
		
	}else{
		$api.removeCls($api.byId('i1'), 'am-icon-angle-up');
		$api.addCls($api.byId('i1'), 'am-icon-angle-down');
		
		sortname = 'sales';
		sort = 'desc';
		
		getList();
	}
}


function clickPrice(){
	
	var classname = $api.byId('i2').className;
	
	if(classname == 'am-icon-angle-down'){
		$api.removeCls($api.byId('i2'), 'am-icon-angle-down');
		$api.addCls($api.byId('i2'), 'am-icon-angle-up');
		
		sortname = 'price';
		sort = 'asc';
		
		getList();
		
	}else{
		$api.removeCls($api.byId('i2'), 'am-icon-angle-up');
		$api.addCls($api.byId('i2'), 'am-icon-angle-down');
		
		sortname = 'price';
		sort = 'desc';
		
		getList();
	}
}


//打开子类
var a=0;
function clickChilds(){
	if(a==0){
		$api.addCls($api.byId('mulu'), 'am-open');
		$api.addCls($api.byId('childs_ul'), 'am-in');
		a=1;
	}else{
		$api.removeCls($api.byId('mulu'), 'am-open');
		$api.removeCls($api.byId('childs_ul'), 'am-in');
		a=0;
	}

};
